﻿/**********************************************
 * 功能名称：产品作业中心计划成本表
编写人：  杨莹
建立时间： 杨 2010年7月21日
说明：
列表查询产品作业中心成本的汇总信息。按计划价格列示金额
 **************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Data.Linq;
using System.Collections.Generic;

namespace Hisun.View.ReportQuery
{
    public partial class Report_CPZYZXJHCBHZB : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        string CenterIdArray = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                AbcFullAccountPeriod MyFullAp = new AbcFullAccountPeriod();

                MyFullAp.FullAccountPeriodToDropdownlist(DropDownList_Year, DropDownList_Month, Application["accountPeriod"].ToString());


                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();

                CheckBox_WithInventary.Checked = true;

                DropDownList_Product.Items.Clear();
                FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

                DropDownList_Product.Items.Clear();
                if (CenterIdArray == "")
                {

                }
                else
                {
                    string[] CenterIDs = CenterIdArray.Split(',');


                    var myProductList = (from a in db.TABC_JS_ProductInProcess
                                         join b in db.TABC_JS_Product on a.产品ID equals b.产品账户ID
                                         where CenterIDs.Contains(a.作业中心ID.ToString())
                                         && a.可用 == true
                                         orderby a.产品ID
                                         select new
                                         {
                                             a.产品ID,
                                             b.产品账户名称
                                         }).Distinct();


                    foreach (var myProduct in myProductList)
                    {
                        DropDownList_Product.Items.Add(new ListItem(myProduct.产品ID + "|" + myProduct.产品账户名称, myProduct.产品ID));

                    }

                }
            }

        }

        //查询当前操作员对应的作业中心列表
        private void FillData2DropDownList(int CenterID)
        {
            ChildCenterList myCenter = new ChildCenterList(CenterID);

            var myCenterFilters = myCenter.CostCenterlist.Where(p => p.停用 == false);


            foreach (var MylistItem in myCenterFilters)
            {

                CenterIdArray = CenterIdArray + "," + MylistItem.作业中心ID.ToString();

            }


        }

        protected void Button_QueryLine_Click(object sender, EventArgs e)
        {
            string MyAp = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); ;

            string MyProductId = DropDownList_Product.SelectedValue;
            var isValidAccountPeriod = from a in db.TABC_JS_AccountPeriods
                                       where a.会计期间ID == MyAp
                                       select a;
            if (isValidAccountPeriod.Count() == 0)
            {
                Response.Write("<script>alert('不存在指定的会计期间');</script>");
                return;

            }
            else
            {
                CostReport MyReport = new CostReport();
                string UserName = Session[ServerVars.sysSessionUserName].ToString();
                string myHtmlReport = "";
                bool IsConvertProductInput = CheckBox_CovertProductInput.Checked;
                if (CheckBox_WithInventary.Checked == false)
                {
                    myHtmlReport = MyReport.WriteExcel_Report_CPZYZXJHCBHZB(MyProductId, MyAp, UserName, false, IsConvertProductInput);
                }
                else
                {
                    myHtmlReport = MyReport.WriteExcel_Report_CPZYZXJHCBHZB(MyProductId, MyAp, UserName, true, IsConvertProductInput);
                }

                Response.Redirect("/Report/UserReport/" + myHtmlReport + ".html");
            }
        }

        protected void Button_Printe_Click(object sender, EventArgs e)
        {
             string MyAp = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); ;
  
            string MyProductId = DropDownList_Product.SelectedValue;
            var isValidAccountPeriod = from a in db.TABC_JS_AccountPeriods
                                       where a.会计期间ID == MyAp
                                       select a;
            if (isValidAccountPeriod.Count() == 0)
            {
                Response.Write("<script>alert('不存在指定的会计期间');</script>");
                return;

            }
            else
            {
                CostReport MyReport = new CostReport();
                string UserName = Session[ServerVars.sysSessionUserName].ToString();
                string myHtmlReport = "";
                bool IsConvertProductInput = CheckBox_CovertProductInput.Checked;
                if (CheckBox_WithInventary.Checked == false)
                {
                    myHtmlReport = MyReport.WriteExcel_Report_CPZYZXJHCBHZB(MyProductId, MyAp, UserName, false, IsConvertProductInput);
                }
                else
                {
                    myHtmlReport = MyReport.WriteExcel_Report_CPZYZXJHCBHZB(MyProductId, MyAp, UserName, true, IsConvertProductInput);
                }

                HttpResponse response = HttpContext.Current.Response;
                response.Clear();
                response.WriteFile("/Report/UserReport/" + myHtmlReport + ".xls");
                string httpHeader = "attachment;filename= CPZYZXJHCBHZB" + MyAp + ".Xls";
                response.AppendHeader("Content-Disposition", httpHeader);
                response.Flush();   
            }
        }
    }
}
